/********************************************************************************
 * Project Name		[JavaEE_JDBC]
 * File Name     	[MySQLConnector.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package utils.connectors;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * MySQL Database Connector
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class MySQLConnector {

	/**
	 * 获得MySQL数据库连接
	 * 
	 * @param driverClassName
	 *            连接数据库的驱动类
	 * @param serverHost
	 *            数据库所在服务器的IP地址或域名
	 * @param serverPort
	 *            数据库所在服务器的端口
	 * @param databaseInstance
	 *            数据库名称
	 * @param username
	 *            登录数据库的用户名
	 * @param password
	 *            登录数据库的密码
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static Connection getMySQLConnection(String driverClassName, String serverHost, String serverPort,
			String databaseInstance, String username, String password) throws ClassNotFoundException, SQLException {
		if (driverClassName == null) {
			driverClassName = "com.mysql.jdbc.Driver";
		}
		if (serverHost == null) {
			serverHost = "127.0.0.1";
		}
		if (serverPort == null) {
			serverPort = "3306";
		}
		String databaseURL = "jdbc:mysql://" + serverHost + ":" + serverPort + "/" + databaseInstance;

		Class.forName(driverClassName);
		return DriverManager.getConnection(databaseURL, username, password);
	}
}
